<!--
 * @Descripttion: 分类管理
 * @Author: xiao li
 * @Date: 2020-12-23 14:46:11
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 14:19:37
-->
<template>
  <div class="lb-website-video">
    <top-nav :title="navTitle" :isBack="true"></top-nav>
    <div class="page-main">
        <el-row class="page-top-operate">
          <lb-button type='primary' @click="switchTreeOpen">全部展开/关闭</lb-button>
          <lb-button type='primary' icon='el-icon-plus' @click="$router.push(`/website/offer/editClassify?modular_id=${searchForm.modular_id}`)">{{$t('menu.WebsiteOfferClassifyAdd')}}</lb-button>
          <lb-button type='danger' icon='el-icon-picture-outline' @click="$router.push(`/website/offer/banner?modular_id=${searchForm.modular_id}`)">{{$t('menu.WebsiteOfferBanner')}}</lb-button>
          <lb-button type='success' icon='el-icon-menu' @click="$router.push(`/website/offer/screen?modular_id=${searchForm.modular_id}`)">{{$t('menu.WebsiteOfferScreen')}}</lb-button>
          <lb-button type='warning' icon='el-icon-user' @click="$router.push(`/website/offer/star?modular_id=${searchForm.modular_id}`)">{{$t('menu.WebsiteOfferStar')}}</lb-button>
         </el-row>

          <el-table
          v-if="showTable"
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            tooltip-effect="dark"
            style="width: 100%"
            row-key="id"
            :default-expand-all='isTreeOpen'
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
           >
            <el-table-column
              prop="id"
              label="ID">
            </el-table-column>
            <el-table-column
              prop="pid"
              label="父ID">
            </el-table-column>
            <el-table-column
              prop="title"
              label="分类名">
            </el-table-column>
            <el-table-column
              prop="title"
              label="封面图">
                <template slot-scope="scope">
                  <lb-image :src="scope.row.cover" />
                </template>
            </el-table-column>
            <el-table-column
              prop="top"
              label="排序值">
            </el-table-column>
            <el-table-column
              prop="address"
              label="是否上架">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="upperGoodsClassify(scope.row.id,scope.row.status)"
                    >
                  </el-switch>
                </template>
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="创建时间">
              <template slot-scope="scope">
                <div>{{scope.row.create_time | handleTime(1)}}</div>
                <div>{{scope.row.create_time | handleTime(2)}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <lb-button size='mini' plain type='primary' @click="$router.push(`/website/offer/editClassify?modular_id=${searchForm.modular_id}&id=${scope.row.id}`)">{{$t('action.edit')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="delGoodsClassify(scope.row.id)">{{$t('action.delete')}}</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      searchForm: {
        modular_id: 0
        // page: 1,
        // limit: 10
      },
      loading: false,
      tableData: [],
      isTreeOpen: true,
      multipleSelection: [],
      showTable: true
    }
  },
  activated () {
    let { id, title } = this.$route.query
    this.searchForm.modular_id = id
    this.navTitle = title
    this.getTableDataList()
  },
  components: {
  },
  methods: {
    /**
     * @method 获取商品分类列表
     */
    getTableDataList () {
      this.loading = true
      this.$api.websiteStarCateList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data
        }
      })
    },
    /**
     * @method 删除表格分类值
     */
    delGoodsClassify (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.websiteStarCateUpdate({id, status: -1}).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.getTableDataList()
          }
        })
      }).catch(() => {

      })
    },
    /**
     * @method 上下架商品分类
     */
    upperGoodsClassify (id, status) {
      this.$api.websiteStarCateUpdate({id, status}).then(res => {
        console.log(res)
        if (res.code === 200) {
          if (status === 0) {
            let {tableData} = this
            this.handleData(tableData, id, 'edit')
            this.tableData = tableData
          }
          this.$message.success(this.$t('tips.successOper'))
        }
      })
    },
    /**
     * @method 处理上下架数据
     */
    handleData (data, id = '', type) {
      if (id) {
        data.forEach((item, index) => {
          if (item.id === id) {
            if (type === 'del') {
              data.splice(index, 1)
            } else {
              item.status = 0
              if (item.children) this.handleData(item.children, '', 'edit')
            }
          }
        })
      } else {
        data.forEach(item => {
          item.status = 0
          if (item.children) this.handleData(item.children, 'edit')
        })
      }
    },
    switchTreeOpen () {
      this.isTreeOpen = !this.isTreeOpen
    }
  },
  watch: {
    isTreeOpen () {
      this.showTable = false
      this.$nextTick(() => {
        this.showTable = true
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-website-video {
  width: 100%;
  .page-main {
    width: 100%;
    .el-input-number {
      width: 270px;
    }
  }
}
</style>
